package com.baidu.yyb.dataBase;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.baidu.yyb.bean.UserData;
import com.baidu.yyb.constant.StringName;

/**
 * 数据库操作
 * @author LiuTong
 */
public class Dbo {
    private final int USERID_COL = 0;
    private final int USER_PASSWORD_COL = 1;
    private final int USER_PHONE_COL = 2;
    private static volatile  Dbo instance;
    private DbHelper dbHelper;
    private Dbo(){
        dbHelper = new DbHelper();
    }
    public static Dbo getInstance(){
        if(instance == null){
            synchronized (Dbo.class){
                if(instance == null){
                    instance = new Dbo();
                }
            }
        }
        return instance;
    }

    public boolean insertUSer(UserData userData) {
        if(userData == null){
            return false;
        }
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(StringName.DB_NOTE_USERID, userData.getUserId());
        contentValues.put(StringName.DB_NOTE_PASSWORD, userData.getPassword());
        contentValues.put(StringName.DB_NOTE_PHONE, userData.getPhoneNumber());
        long result =  db.insert(StringName.DB_USER_TABLE, null, contentValues);
        contentValues.clear();
        db.close();
        if(result != -1){
            return true;
        }
        return false;
    }

    public void changePhone(String oldPhone, String userId, String userPassword, String newPhoneNumber) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String sql = "update " + StringName.DB_USER_TABLE + " set '"+ StringName.DB_NOTE_PHONE+"'= ? " +
                "where '"+StringName.DB_NOTE_USERID+"' = ?" +
                " and '"+StringName.DB_NOTE_PASSWORD+"' =?" +
                " and '"+StringName.DB_NOTE_PHONE+"'=?";
        db.execSQL(sql, new Object[]{newPhoneNumber, userId, userPassword, oldPhone});
        db.close();
    }


    public void changePassword(String userId,String oldPassword, String newPassword) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String sql = "update " + StringName.DB_USER_TABLE + " set '"+StringName.DB_NOTE_PASSWORD+"'= ? " +
                "where  '"+StringName.DB_NOTE_USERID+"' =? and '"+StringName.DB_NOTE_PASSWORD+"' = ?";
        db.execSQL(sql, new Object[]{newPassword, userId,oldPassword});
        db.close();
    }

    public boolean changeInfoWithId(UserData userData){
        if(userData == null){
            return false;
        }
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String valuesCause = StringName.DB_NOTE_USERID + "=?";
        ContentValues contentValues = new ContentValues();
        contentValues.put(StringName.DB_NOTE_USERID,userData.getUserId());
        contentValues.put(StringName.DB_NOTE_PASSWORD,userData.getPassword());
        contentValues.put(StringName.DB_NOTE_PHONE,userData.getPhoneNumber());
        int result = db.update(StringName.DB_USER_TABLE,contentValues,valuesCause, (String[]) contentValues.get(StringName.DB_NOTE_USERID));
        contentValues.clear();
        db.close();
        return result > 0;
    }


    public String getPassword(String userId) {
        String strMM = "";
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String sql = "select * from " + StringName.DB_USER_TABLE + " where '"+StringName.DB_NOTE_USERID+"' = ?";
        Cursor cursor = db.rawQuery(sql, new String[]{userId});

        while (cursor.moveToNext()) {
            strMM = cursor.getString(USER_PASSWORD_COL);
        }
        db.close();
        return strMM;
    }

    /***
     * 获取账号
     * @return
     */
    public String getUserId() {
        String strMM = "";
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String sql = "select * from " + StringName.DB_USER_TABLE + " ";
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            strMM = cursor.getString(USERID_COL);
        }
        db.close();
        return strMM;
    }

    /***
     * 获取数据库中的手机号
     * @return
     */
    public String getPhone(String userId) {
        String strMM = "";
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String sql = "select * from " + StringName.DB_USER_TABLE + " where '"+StringName.DB_NOTE_USERID+"' = ?";
        Cursor cursor = db.rawQuery(sql, new String[]{userId});
        while (cursor.moveToNext()) {
            strMM = cursor.getString(USER_PHONE_COL);
        }
        db.close();
        return strMM;
    }

}
